package com.songcha.library_common.util

import android.R.attr.bitmap
import android.content.Context
import android.graphics.Bitmap
import android.graphics.Canvas
import android.graphics.Matrix
import android.graphics.PixelFormat
import android.graphics.Rect
import android.graphics.drawable.BitmapDrawable
import android.graphics.drawable.Drawable
import android.view.View
import androidx.core.graphics.drawable.toBitmap
import java.io.ByteArrayOutputStream


class BitmapUtil {
    companion object {

        @JvmStatic
        fun bitmap2Drawable(context: Context,bitmap: Bitmap):Drawable{
            return BitmapDrawable(context.resources,bitmap)
        }

        @JvmStatic
        fun drawable2Bitmap(drawable: Drawable):Bitmap{
            return drawable.toBitmap()

            //return BitmapFactory.decodeResource(context.resources,resId)
            //声明将要创建的bitmap
            /*var bitmap: Bitmap? = null
            //获取图片宽度
            val width: Int = drawable.getIntrinsicWidth()
            //获取图片高度
            val height: Int = drawable.getIntrinsicHeight()
            //图片位深，PixelFormat.OPAQUE代表没有透明度，RGB_565就是没有透明度的位深，否则就用ARGB_8888。详细见下面图片编码知识。
            val config =
                if (drawable.getOpacity() !== PixelFormat.OPAQUE) Bitmap.Config.ARGB_8888 else Bitmap.Config.RGB_565
            //创建一个空的Bitmap
            bitmap = Bitmap.createBitmap(width, height, config)
            //在bitmap上创建一个画布
            val canvas = Canvas(bitmap)
            //设置画布的范围
            drawable.setBounds(0, 0, width, height)
            //将drawable绘制在canvas上
            drawable.draw(canvas)
            return bitmap*/
        }

        @JvmStatic
        fun view2Bitmap(view: View): Bitmap? {
            val rect = Rect().apply {
                left = 0
                top = 0
                right = view.width
                bottom = view.height
            }
            return view2Bitmap(view,rect,1f)
        }
        /**
         *
         * get view crop rect efficiently
         * @param view
         * @param crop
         * @param downscaleFactor
         * @return
         */
        @JvmStatic
        fun view2Bitmap(view: View, crop: Rect, downscaleFactor: Float): Bitmap? {
            val screenView: View = view
            val width = (crop.width() * downscaleFactor).toInt()
            val height = (crop.height() * downscaleFactor).toInt()
            val dx: Float = -crop.left * downscaleFactor
            val dy: Float = -crop.top * downscaleFactor
            if (width * height <= 0) {
                return null
            }
            val bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888)
            val canvas = Canvas(bitmap)
            canvas.density = ScreenUtil.getDensityDpi()
            val matrix = Matrix()
            matrix.preScale(downscaleFactor, downscaleFactor)
            matrix.postTranslate(dx, dy)
            canvas.setMatrix(matrix)
            screenView.draw(canvas)
            return bitmap
        }

        @JvmStatic
        fun bitmap2ByteArray(bitmap:Bitmap):ByteArray{
            val stream = ByteArrayOutputStream()
            bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream)
            val byteArray: ByteArray = stream.toByteArray()
            return byteArray
        }


    }
}